diff --git a/swh/web/common/swh_templatetags.py b/swh/web/common/swh_templatetags.py
index 43ca3941..091121f0 100644
--- a/swh/web/common/swh_templatetags.py
+++ b/swh/web/common/swh_templatetags.py
@@ -1,82 +1,82 @@
# Copyright (C) 2017 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import re
from docutils.core import publish_parts
from docutils.writers.html4css1 import Writer, HTMLTranslator
from inspect import cleandoc
from django import template
register = template.Library()
class NoHeaderHTMLTranslator(HTMLTranslator):
"""
Docutils translator subclass to customize the generation of HTML
from reST-formatted docstrings
"""
def __init__(self, document):
super().__init__(document)
self.body_prefix = []
self.body_suffix = []
def visit_bullet_list(self, node):
self.context.append((self.compact_simple, self.compact_p))
self.compact_p = None
self.compact_simple = self.is_compactable(node)
self.body.append(self.starttag(node, 'ul', CLASS='docstring'))
DOCSTRING_WRITER = Writer()
DOCSTRING_WRITER.translator_class = NoHeaderHTMLTranslator
@register.filter
def safe_docstring_display(docstring):
"""
Utility function to htmlize reST-formatted documentation in browsable
api.
"""
docstring = cleandoc(docstring)
return publish_parts(docstring, writer=DOCSTRING_WRITER)['html_body']
@register.filter
-def urlize_api_links(text):
+def urlize_links_and_mails(text):
"""Utility function for decorating api links in browsable api.
Args:
text: whose content matching links should be transformed into
contextual API or Browse html links.
Returns
The text transformed if any link is found.
The text as is otherwise.
"""
text = re.sub(r'(/api/[^"<]*/|/browse/.*/|http.*$)',
r'\1',
text)
return re.sub(r'([^ <>"]+@[^ <>"]+)',
r'\1',
text)
@register.filter
def urlize_header_links(text):
"""Utility function for decorating headers links in browsable api.
Args
text: Text whose content contains Link header value
Returns:
The text transformed with html link if any link is found.
The text as is otherwise.
"""
return re.sub(r'<(/api/.*|/browse/.*)>', r'<\1>',
text)
diff --git a/swh/web/templates/apidoc.html b/swh/web/templates/apidoc.html
index 55d9ae11..753eb1a6 100644
--- a/swh/web/templates/apidoc.html
+++ b/swh/web/templates/apidoc.html
@@ -1,149 +1,149 @@
{% extends "layout.html" %}
{% load swh_templatetags %}
{% load static %}
{% block title %}{{ heading }} – Software Heritage API {% endblock %}
{% block header %}
{% endblock %}
{% block content %}
{% if docstring %}
{% endblock %}
diff --git a/swh/web/tests/common/test_templatetags.py b/swh/web/tests/common/test_templatetags.py
index 29f36f85..67e5e56f 100644
--- a/swh/web/tests/common/test_templatetags.py
+++ b/swh/web/tests/common/test_templatetags.py
@@ -1,66 +1,66 @@
# Copyright (C) 2015-2017 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
import unittest
from nose.tools import istest
from swh.web.common import swh_templatetags
class SWHTemplateTagsTest(unittest.TestCase):
@istest
def urlize_api_links_api(self):
# update api link with html links content with links
content = '{"url": "/api/1/abc/"}'
expected_content = ('{"url": "/api/1/abc/"}')
- self.assertEquals(swh_templatetags.urlize_api_links(content),
+ self.assertEquals(swh_templatetags.urlize_links_and_mails(content),
expected_content)
@istest
def urlize_api_links_browse(self):
# update /browse link with html links content with links
content = '{"url": "/browse/def/"}'
expected_content = ('{"url": "'
'/browse/def/"}')
- self.assertEquals(swh_templatetags.urlize_api_links(content),
+ self.assertEquals(swh_templatetags.urlize_links_and_mails(content),
expected_content)
@istest
def urlize_header_links(self):
# update api link with html links content with links
content = """; rel="next"
; rel="prev"
"""
expected_content = """</api/1/abc/>; rel="next"
</api/1/def/>; rel="prev"
"""
self.assertEquals(swh_templatetags.urlize_header_links(content),
expected_content)
@istest
def safe_docstring_display(self):
# update api link with html links content with links
docstring = """This is my list header:
- Here is item 1, with a continuation
line right here
- Here is item 2
Here is something that is not part of the list"""
expected_docstring = """
This is my list header:
Here is item 1, with a continuation
line right here